﻿﻿using System;
﻿using System.Data;
using System.Web.UI;
﻿using System.Globalization;
﻿using Pantry.Business;
using Pantry.Commons;


namespace Pantry.Web.Users
{
    public partial class ViewListContact : System.Web.UI.Page
    {
        private readonly IFormatProvider _formatProvider = new CultureInfo("en-gb");
        private ContactBus _contactBus;
        private DataView _dataView;
        private static bool _isSort = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            //LoadContact(CheckBoxIsActive.Checked);
            FilterContact(TextBoxFirstName.Text, TextBoxSurname.Text, CheckBoxIsActive.Checked);
        }
        /// <summary>
        /// Filters the contact.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="surName">Name of the sur.</param>
        /// <param name="isIncludeInactiveContact">if set to <c>true</c> [is include inactive contact].</param>
        private void FilterContact(string firstName, string surName, bool isIncludeInactiveContact)
        {
            try
            {
                _contactBus = new ContactBus();
                DataTable dataTable = _contactBus.ListContact();
                _dataView = dataTable.DefaultView;
                string statement = string.Format(_formatProvider, "FullName like '%{0}%' AND FullName LIKE '%{1}%'", firstName, surName);

                if (!isIncludeInactiveContact)
                {
                    statement += "AND IsActive = 'true'";
                }
                _dataView.RowFilter = statement;
                CollectionPager1.PageSize = 10;
                CollectionPager1.BindToControl = RepeaterContact;
                CollectionPager1.DataSource = _dataView;
                RepeaterContact.DataSource = CollectionPager1.DataSourcePaged;
                RepeaterContact.DataBind();
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterContact(TextBoxFirstName.Text, TextBoxSurname.Text, CheckBoxIsActive.Checked);
        }


        protected void ButtonNoneClick(Object sender, EventArgs e)
        {
            TextBoxFirstName.Text = string.Empty;
            TextBoxSurname.Text = string.Empty;
            LoadContact(CheckBoxIsActive.Checked);
        }

        protected void ButtonCreateClick(Object sender, EventArgs e)
        {
            Response.Redirect("CreateContact.aspx");
        }

        protected void ButtonClearClick(Object sender, EventArgs e)
        {
            TextBoxFirstName.Text = string.Empty;
            TextBoxSurname.Text = string.Empty;

        }

        protected void ButtonEditClick(Object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(HttpHelper.GetFormString("RadioContactId")))
            {
                int contactId = int.Parse(HttpHelper.GetFormString("RadioContactId"), _formatProvider);
                Response.Redirect("EditContact.aspx?id=" + contactId);

            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", 
                    "alert('You must select a contact to edit.');", true);
            }
        }

        protected void CheckBoxIsActiveCheckedChanged(object sender, EventArgs e)
        {
            FilterContact(TextBoxFirstName.Text, TextBoxSurname.Text, CheckBoxIsActive.Checked);
        }

        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            //Todo code here
        }
        /// <summary>
        /// Loads the contact.
        /// </summary>
        /// <param name="isIncludeInactiveContact">if set to <c>true</c> [is include inactive contact].</param>
        private void LoadContact(bool isIncludeInactiveContact)
        {
            try
            {
                _contactBus = new ContactBus();
                DataTable dataTable = _contactBus.ListContact();
                _dataView = dataTable.DefaultView;
                if (!isIncludeInactiveContact)
                {
                    _dataView.RowFilter = "IsActive = 'true'";
                }
                CollectionPager1.PageSize = 10;
                CollectionPager1.BindToControl = RepeaterContact;
                CollectionPager1.DataSource = _dataView;
                RepeaterContact.DataSource = CollectionPager1.DataSourcePaged;
                RepeaterContact.DataBind();

            }
            catch (ArgumentNullException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
            }
        }
        protected void ButtonSortByNameClick(object sender, EventArgs e)
        {
            SortContact("FullName");
        }
        protected void ButtonSortByMobileClick(object sender, EventArgs e)
        {
            SortContact("MobilePhone");
        }
        protected void ButtonSortByEmailClick(object sender, EventArgs e)
        {
            SortContact("Email");
        }
        protected void ButtonSortByStatusClick(object sender, EventArgs e)
        {
            SortContact("IsActive");
        }
        protected void ButtonSortByContactTypeClick(object sender, EventArgs e)
        {
            SortContact("ContactType");
        }
        /// <summary>
        /// Shorts the contact.
        /// </summary>
        /// <param name="field">The field.</param>
        private void SortContact(string field)
        {
            try
            {
                if (_isSort)
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} DESC", field);
                    _isSort = false;
                }
                else
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} ASC", field);
                    _isSort = true;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

    }
}